#include <cassert>
#include <string>
#include <unordered_set>

using namespace std;

class Solution {
public:
    string reverseVowels(string s) {
        if (s.empty()) {
            return s;
        }

        int n = s.size();
        int left = 0;
        int right = n - 1;
        while (left < right) {
            while (left < n && !isVowels(s[left])) {
                ++left;
            }
            while (right > 0 && !isVowels(s[right])) {
                --right;
            }
            if (left < right) {
                swap(s[left], s[right]);
                ++left;
                --right;
            }
        }
        return s;
    }

private:
    unordered_set<char> vowels = {
        'a', 'e', 'i', 'o', 'u', 'A', 'E', 'I', 'O', 'U'};

    bool isVowels(char ch) {  // 用set来做搜索相较string更快
        return vowels.find(ch) != vowels.end();
    }
};

void test() {
    Solution s;

    string input1 = "hello";
    string input2 = "leetcode";
    string output1 = "holle";
    string output2 = "leotcede";

    assert(s.reverseVowels(input1) == output1);
    assert(s.reverseVowels(input2) == output2);
}

int main() {
    test();
    return 0;
}